package cn.schoolwow.workflow.module.instance.query.kit;

import cn.schoolwow.quickdao.domain.database.dql.condition.Condition;
import cn.schoolwow.quickdao.domain.database.dql.response.PageVo;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.workflow.domain.instance.InstanceContextDataRequest;
import cn.schoolwow.workflow.domain.instance.WorkFlowInstanceQuery;
import cn.schoolwow.workflow.entity.WorkFlowInstance;
import cn.schoolwow.workflow.entity.WorkFlowInstanceHistory;
import cn.schoolwow.workflow.module.common.domain.QuickWorkFlowConfig;
import cn.schoolwow.workflow.module.instance.query.flow.GetWorkFlowInstanceConditionFlow;
import cn.schoolwow.workflow.module.instance.query.flow.GetWorkFlowInstanceHistoryListFlow;
import cn.schoolwow.workflow.module.instance.query.service.GetInstanceContextDataFlow;
import com.alibaba.fastjson.JSONObject;

import java.util.List;

public class WorkFlowInstanceQueryServiceImpl implements WorkFlowInstanceQueryService {
    private QuickWorkFlowConfig quickWorkFlowConfig;

    public WorkFlowInstanceQueryServiceImpl(QuickWorkFlowConfig quickWorkFlowConfig) {
        this.quickWorkFlowConfig = quickWorkFlowConfig;
    }

    @Override
    public long getWorkFlowInstanceCount(WorkFlowInstanceQuery workFlowInstanceQuery) {
        FlowContext flowContext = quickWorkFlowConfig.workflow.startFlow(new GetWorkFlowInstanceConditionFlow())
                .putInstanceData(workFlowInstanceQuery)
                .execute();
        Condition condition = flowContext.checkInstanceData(Condition.class);
        return condition.execute().count();
    }

    @Override
    public WorkFlowInstance getSingleWorkFlowInstance(Long workFlowInstanceId) {
        WorkFlowInstanceQuery workFlowInstanceQuery = new WorkFlowInstanceQuery();
        workFlowInstanceQuery.instanceIdList.add(workFlowInstanceId);
        return getSingleWorkFlowInstance(workFlowInstanceQuery);
    }

    @Override
    public WorkFlowInstance getSingleWorkFlowInstance(WorkFlowInstanceQuery workFlowInstanceQuery) {
        PageVo<WorkFlowInstance> pageVo = getWorkFlowInstancePagingList(workFlowInstanceQuery,1,1);
        if(!pageVo.getList().isEmpty()){
            return pageVo.getList().get(0);
        }
        return null;
    }

    @Override
    public PageVo<WorkFlowInstance> getWorkFlowInstancePagingList(WorkFlowInstanceQuery workFlowInstanceQuery, int pageNumber, int pageSize) {
        FlowContext flowContext = quickWorkFlowConfig.workflow.startFlow(new GetWorkFlowInstanceConditionFlow())
                .putInstanceData(workFlowInstanceQuery)
                .execute();
        Condition condition = flowContext.checkInstanceData(Condition.class);
        PageVo<WorkFlowInstance> pageVo = condition
                .page(pageNumber, pageSize)
                .execute()
                .getPagingList();
        return pageVo;
    }

    @Override
    public List<WorkFlowInstance> getWorkFlowInstanceList(WorkFlowInstanceQuery workFlowInstanceQuery) {
        FlowContext flowContext = quickWorkFlowConfig.workflow.startFlow(new GetWorkFlowInstanceConditionFlow())
                .putInstanceData(workFlowInstanceQuery)
                .execute();
        Condition condition = flowContext.checkInstanceData(Condition.class);
        List<WorkFlowInstance> workFlowInstanceList = condition.execute().getList();
        return workFlowInstanceList;
    }

    @Override
    public JSONObject getWorkFlowInstanceContextData(Long workFlowInstanceId) {
        InstanceContextDataRequest contextDataRequest = new InstanceContextDataRequest();
        contextDataRequest.workFlowInstanceId = workFlowInstanceId;
        return getWorkFlowInstanceContextData(contextDataRequest);
    }

    @Override
    public JSONObject getWorkFlowInstanceContextData(InstanceContextDataRequest contextDataRequest){
        FlowContext flowContext = quickWorkFlowConfig.workflow.startFlow(new GetInstanceContextDataFlow())
                .putInstanceData(contextDataRequest)
                .execute();
        JSONObject contextData = (JSONObject) flowContext.getData("contextData");
        return contextData;
    }

    @Override
    public List<WorkFlowInstanceHistory> getWorkFlowInstanceHistoryList(WorkFlowInstanceQuery workFlowInstanceQuery) {
        FlowContext flowContext = quickWorkFlowConfig.workflow.startFlow(new GetWorkFlowInstanceConditionFlow())
                .next(new GetWorkFlowInstanceHistoryListFlow())
                .putInstanceData(workFlowInstanceQuery)
                .execute();
        List<WorkFlowInstanceHistory> list = flowContext.getData("list", List.class);
        return list;
    }

}
